// pages/shopping/shopping.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
     cart:[],
     selectAll:false,
     totalPrice:0,
     totalPiece:0,
     touch:{
       id: 0,
       start: 0
     }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { 

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onRefresh()
  },

  switchToDetail:function(e){
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },

  onRefresh: function () {
    wx.getStorage({
      key: 'cart',
      success: res=> {
        let cart = res.data
        for (var i in cart) {
          cart[i].selected = true
          cart[i].showDelBtn = false
        }
        this.setData({
          cart: cart
        })
        this.onSummary()
      }
    })
    
  },

  onSummary: function(){
    let cart = this.data.cart
    let selectAll = false
    let totalPrice = 0
    let totalPiece = 0
    for (var i in cart) {
      if (cart[i].selected){
        totalPrice += cart[i].price * cart[i].total
        totalPiece += 1
      }
    }
    if (cart.length == totalPiece){
      selectAll = true
    }
    this.setData({
      cart:cart,
      selectAll: selectAll,
      totalPrice: Number(totalPrice).toFixed(2),
      totalPiece: totalPiece,
    })
    wx.removeTabBarBadge({
      index: 3,
    })
    if(this.data.cart.length) {
      wx.setTabBarBadge({
        index: 3,
        text: String(this.data.cart.length),
      })
    }
  },

onSelectAll: function() {
   let cart = this.data.cart
   if (this.data.selectAll){
     for (var i in cart){
       cart[i].selected =false
     }
   }else{
     for (var [i] in cart){
       cart[i].selected = true
     }
   }
   this.onSummary()
},

onSelectCard: function(e) {
  let cart = this.data.cart
  for (var i in cart) {
    if (cart[i].id == e.target.dataset.id) {
      cart[i].selected = !cart[i].selected
    }
  }
  this.setData({
    cart: cart
  })
  this.onSummary()
},

subnum: function(e) {
   let cart = this.data.cart
   for (var i in cart) {
     if (cart[i].id == e.currentTarget.dataset.id) {
       cart[i].total = cart[i].total <= 1 ? 1 : --cart[i].total
     }
   }
   this.setData({
     cart: cart
   })
   this.onSummary()
},

addnum: function(e) {
  let cart = this.data.cart
  for (var i in cart) {
    if (cart[i].id == e.currentTarget.dataset.id) {
      cart[i].total = ++cart[i].total
    }
  }
  this.setData({
    cart: cart
  })
  this.onSummary()
},

onTouchStart: function(e) {
  let touch = {
    id: e.currentTarget.dataset.id,
    start: e.changedTouches["0"].clientX
  }
  let cart = this.data.cart
  for (var i in cart) {
    cart[i].showDelBtn = false
  }
  this.setData({
    cart: cart,
    touch: touch
  })
},

onTouchMove: function(e) {
  let cart = this.data.cart
  if (this.data.touch.start - e.changedTouches["0"].clientX > 100) {
    for (var i in cart) {
      if (this.data.touch.id == cart[i].id) {
        cart[i].showDelBtn = true
      }
    }
  }else if (e.changedTouches["0"].clientX - this.data.touch.start > 100) {
    for ( var i in cart){
      if (this.data.touch.id == cart[i].id){
        cart[i].showDelBtn = false
      }
    }
  }
  this.setData({
    cart: cart
  })
},

onDeleteCard: function(e) {
  let cart = this.data.cart
  for (var i in cart) {
    if (cart[i].id == e.target.dataset.id) {
      cart.splice(i, 1)
      wx.setStorage({
        data: cart,
        key: 'cart',
      })
    }
  }
  this.setData({
    cart:cart
  })
  this.onSummary()
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})